from manimlib import *

logo_color = [WHITE, "CC0000"]


class logo_black(Scene):
    def construct(self):
        value = ValueTracker()
        func = FunctionGraph(lambda x: (x**2)**(1/3)+.9*np.sqrt(abs(8-x*x))*np.sin(
            value.get_value()*x), x_range=[-np.sqrt(8), np.sqrt(8), .001]).set_color(WHITE)
        self.play(ShowCreation(VGroup(func)))
        func.add_updater(lambda M: M.become(FunctionGraph(lambda x: (x**2)**(1/3)+.9*np.sqrt(abs(8-x*x))
                         * np.sin(value.get_value()*x), x_range=[-np.sqrt(8), np.sqrt(8), .01]).set_color(WHITE)))
        self.add(func, value)
        self.play(value.animate.set_value(10), run_time=3)
        func_white = FunctionGraph(lambda x: (x**2)**(1/3)+.9*np.sqrt(abs(8-x*x))*np.sin(
            value.get_value()*x*x), x_range=[-np.sqrt(8), np.sqrt(8), .001]).set_color(logo_color[0])
        func_red = FunctionGraph(lambda x: (x**2)**(1/3)+.9*np.sqrt(abs(8-x*x))*np.sin(value.get_value()/x/x), x_range=[-np.sqrt(
            8), np.sqrt(8), .001], use_smoothing=False, discontinuities=[0]).set_color(logo_color[1])
        func.clear_updaters()
        func.become(FunctionGraph(lambda x: (x**2)**(1/3)+.9*np.sqrt(abs(8-x*x))*np.sin(
            value.get_value()*x), x_range=[-np.sqrt(8), np.sqrt(8), .001]).set_color(WHITE))
        self.remove(func)
        white = func_white.get_bezier_tuples()
        red = func_red.get_bezier_tuples()
        ori = func.get_bezier_tuples()
        a = VGroup(*[VMobject().set_anchors_and_handles(*white[i])
                   for i in range(len(white))])
        b = VGroup(*[VMobject().set_anchors_and_handles(*red[i])
                   for i in range(len(red))]).set_color(logo_color[1])
        c = VGroup(*[VMobject().set_anchors_and_handles(*ori[i])
                   for i in range(len(ori))])
        self.play(LaggedStart(*[ReplacementTransform(i, j) for i, j in zip(c, a)], lag_ratio=.001), LaggedStart(
            *[ReplacementTransform(i, j) for i, j in zip(c.copy(), b)], lag_ratio=.002), run_time=3)
        #self.add(a,b)
        #self.remove(c)
        self.wait()
        self.remove(a, b)
        self.add(func_white, func_red)
        self.play(FadeOut(func_white), FadeOut(func_red))
